package com.mc.fil.service.impl;

import java.util.List;
import java.util.UUID;

import com.alibaba.fastjson.JSONObject;
import com.mc.common.constant.Constants;
import com.mc.common.core.domain.entity.SysUser;
import com.mc.common.core.domain.model.LoginUser;
import com.mc.common.gisserver.GisService;
import com.mc.common.utils.DateUtils;
import com.mc.common.utils.ServletUtils;
import com.mc.fil.domain.FilBase;
import com.mc.fil.domain.FilUserRelation;
import com.mc.fil.mapper.FilBaseMapper;
import com.mc.fil.mapper.FilUserRelationMapper;
import com.mc.framework.web.service.TokenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.mc.fil.mapper.FilPlantInfoMapper;
import com.mc.fil.domain.FilPlantInfo;
import com.mc.fil.service.IFilPlantInfoService;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;

/**
 * 种植信息 Service业务层处理
 *
 * @author mc
 * @date 2021-04-12
 */
@Service
public class FilPlantInfoServiceImpl implements IFilPlantInfoService
{
    @Autowired
    private FilPlantInfoMapper filPlantInfoMapper;
    @Autowired
    private TokenService tokenService;
    @Autowired
    private GisService gisService;
    @Autowired
    private FilUserRelationMapper userRelationMapper;
    @Autowired
    private FilBaseMapper baseMapper;

    /**
     * 查询种植信息
     *
     * @param id 种植信息 ID
     * @return 种植信息
     */
    @Override
    public FilPlantInfo selectFilPlantInfoById(String id)
    {
        FilPlantInfo plantInfo = filPlantInfoMapper.selectFilPlantInfoById(id);
        FilUserRelation filUserRelation = new FilUserRelation();
        filUserRelation.setFilId(id);
        plantInfo.setDutyUserId(userRelationMapper.selectFilUserRelationList(filUserRelation));
        return plantInfo;
    }

    /**
     * 查询种植信息 列表
     *
     * @param filPlantInfo 种植信息
     * @return 种植信息
     */
    @Override
    public List<FilPlantInfo> selectFilPlantInfoList(FilPlantInfo filPlantInfo)
    {
        List<FilPlantInfo> list = filPlantInfoMapper.selectFilPlantInfoList(filPlantInfo);
        for(FilPlantInfo filPlantInfo1 : list){
            FilUserRelation filUserRelation = new FilUserRelation();
            filUserRelation.setFilId(filPlantInfo.getId());
            filPlantInfo1.setDutyUserId(userRelationMapper.selectFilUserRelationList(filUserRelation));
        }

        return list;
    }

    /**
     * 新增种植信息
     *
     * @param filPlantInfo 种植信息
     * @return 结果
     */
    @Override
    public int insertFilPlantInfo(FilPlantInfo filPlantInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filPlantInfo.setCreateTime(DateUtils.getNowDate());
        filPlantInfo.setCreateUserIf(loginUser.getUsername());
        filPlantInfo.setStatus("0");
        FilBase filBase = baseMapper.selectFilBaseById(filPlantInfo.getBaseId());
        filPlantInfo.setOwnership(filBase.getName());
        String uuid = UUID.randomUUID().toString().replaceAll("-","");
        MultiValueMap<String, String> params1= new LinkedMultiValueMap<String, String>();
        params1.add("features", "[{\"attributes\": {\n" +
                "    \"TYPE\": \""+filPlantInfo.getType()+"\",\n" +
                "    \"ID\": \""+uuid+"\",\n" +
                "    \"PART_NAME \": \""+filPlantInfo.getPartName()+"\",\n" +
                "    \"BASE_ID\": \""+filPlantInfo.getBaseId()+"\"\n" +
                "   },\n" +
                "   \"geometry\": {\n" +
                "    \"rings\": [\n" +
                filPlantInfo.getRings()+
                "   ]\n" +
                "   }}]");
        try{
            String jsonObject1 = gisService.restTemplate(params1, Constants.GIS_SERVER_SEED_FARM_URL2+"addFeatures");
            String object1 = JSONObject.parseObject(jsonObject1).getJSONArray("addResults").getJSONObject(0).getString("objectId");
            if(object1!=null&&!"".equals(object1)){
                List<FilUserRelation> userList = filPlantInfo.getDutyUserId();
                for(FilUserRelation user : userList){
                    user.setFilId(uuid);
                    userRelationMapper.insertFilUserRelation(user);
                }
                filPlantInfo.setId(uuid);
                return filPlantInfoMapper.insertFilPlantInfo(filPlantInfo);
            }
        }catch (Exception e){

        }
        return 0;
    }

    /**
     * 修改种植信息
     *
     * @param filPlantInfo 种植信息
     * @return 结果
     */
    @Override
    public int updateFilPlantInfo(FilPlantInfo filPlantInfo)
    {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filPlantInfo.setUpdateTime(DateUtils.getNowDate());
        filPlantInfo.setUpdateUserId(loginUser.getUsername());


        MultiValueMap<String, String> queryParams= new LinkedMultiValueMap<String, String>();
        queryParams.add("where","ID = \'"+filPlantInfo.getId()+"\'");
        String objectId= gisService.selectObjectId(queryParams,Constants.GIS_SERVER_SEED_FARM_URL2);
        MultiValueMap<String, String> params1= new LinkedMultiValueMap<String, String>();
        if(filPlantInfo.getRings() !=null &&!"".equals(filPlantInfo.getRings())){
            params1.add("features", "[{\"attributes\": {\n" +
                    "    \"TYPE\": \""+filPlantInfo.getType()+"\",\n" +
                    "    \"OBJECTID\": "+objectId+",\n" +
                    "    \"PART_NAME \": \""+filPlantInfo.getPartName()+"\",\n" +
                    "    \"BASE_ID\": \""+filPlantInfo.getBaseId()+"\"\n" +
                    "   },\n" +
                    "   \"geometry\": {\n" +
                    "    \"rings\": [\n" +
                    filPlantInfo.getRings()+
                    "   ]\n" +
                    "   }}]");
        }else{
            params1.add("features", "[{\"attributes\": {\n" +
                    "    \"TYPE\": \""+filPlantInfo.getType()+"\",\n" +
                    "    \"OBJECTID\": "+objectId+",\n" +
                    "    \"PART_NAME \": \""+filPlantInfo.getPartName()+"\",\n" +
                    "    \"BASE_ID\": \""+filPlantInfo.getBaseId()+"\"\n" +
                    "   }\n" +

                    "   }]");
        }

        try{
            String jsonObject1 = gisService.restTemplate(params1, Constants.GIS_SERVER_SEED_FARM_URL2+"updateFeatures");
            String object1 = JSONObject.parseObject(jsonObject1).getJSONArray("updateResults").getJSONObject(0).getString("success");
            if(object1!=null&&"true".equals(object1)){
                userRelationMapper.deleteFilUserRelationById(filPlantInfo.getId());
                List<FilUserRelation> userList = filPlantInfo.getDutyUserId();
                for(FilUserRelation user : userList){
                    userRelationMapper.insertFilUserRelation(user);
                }
                return filPlantInfoMapper.updateFilPlantInfo(filPlantInfo);
            }
        }catch (Exception e){

        }
        return 0;

    }

    /**
     * 删除种植信息 信息
     *
     * @param id 种植信息 ID
     * @return 结果
     */
    @Override
    public int deleteFilPlantInfoById(String id)
    {
        MultiValueMap<String, String> queryParams= new LinkedMultiValueMap<String, String>();
        queryParams.add("where","ID = \'"+id+"\'");
        String objectId= gisService.selectObjectId(queryParams,Constants.GIS_SERVER_SEED_FARM_URL2);
        MultiValueMap<String, String> params = new LinkedMultiValueMap<String, String>();
        params.add("objectIds", objectId);
        String jsonObject = gisService.restTemplate(params, Constants.GIS_SERVER_SEED_FARM_URL2 + "deleteFeatures");
        String object = JSONObject.parseObject(jsonObject).getJSONArray("deleteResults").getJSONObject(0).getString("success");
        try{
            if(object!=null&&"true".equals(object)){
                return filPlantInfoMapper.deleteFilPlantInfoById(id);
            }
        }catch (Exception e){

        }

        return 0;
    }

    @Override
    public int updateFilPlantInfoForRateSurvivalAndGreenRate(FilPlantInfo filPlantInfo) {
        LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
        filPlantInfo.setUpdateTime(DateUtils.getNowDate());
        filPlantInfo.setUpdateUserId(loginUser.getUsername());
        return filPlantInfoMapper.updateFilPlantInfo(filPlantInfo);
    }


}